home *** CD-ROM | disk | FTP | other *** search
- Path: news.eden.com!usenet
- From: Shane Sadler <nexus@eden.com>
- Newsgroups: comp.lang.c
- Subject: Re: Checking for Ilegal Input
- Date: Sat, 02 Mar 1996 20:11:03 -0700
- Organization: Eden Matrix Services
- Message-ID: <31390DC7.DB9DB49@eden.com>
- References: <313855C7.5E86@aol.com>
- NNTP-Posting-Host: net-4-139.austin.eden.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
-
- gregace@aol.com wrote:
- >
- > I have a program where you must enter a number between 0 and 15 and check for
- > illegal input such as a number not between 0 and 15 or if a character is
- > entered.
- >
- > I used the scanf("%d",&number) to read the input.
- >
- > I check the illegal input by
- >
- > while (number<0 or number>15)
- > { Printf("Enter a again:");
- > scanf("%d",&number);
- > }
- >
- > This check works fine accept when the user enters a character such as the
- > letter f. Does anyone know how to check illegal input that is not a number?
-
- Declare a flag that accepts a return value from scanf(). That function
- returns a zero if there was input, but no conversions assigned. That's
- one way to check for invalid input (someone entering "foo" when you're
- looking for a %d conversion). The same holds true for the entire scanf()
- family.
-
- Hope this helps.
-
- -- Shane
- ===================================================================
- S. M. Sadler
- e-mail: nexus@eden.com
- Web: http://www.eden.com/~nexus
-